package com.miyuan.wm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.miyuan.wm.aop.UserOperation;
import com.miyuan.wm.mapper.RespositoryMapper;
import com.miyuan.wm.pojo.RespositoryEntity;
import com.miyuan.wm.request.AddResponsitorysRequestVo;
import com.miyuan.wm.request.QueryRespositorysRequestVo;
import com.miyuan.wm.request.UpdateResponsitorysRequestVo;
import com.miyuan.wm.response.BaseResponse;
import com.miyuan.wm.response.PageResponse;
import com.miyuan.wm.response.QueryRespositorysResponseVo;
import com.miyuan.wm.service.RespositoryService;
import com.miyuan.wm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @ClassName RespositoryServiceImpl
 * @Description 仓库管理业务实现
 */
@Service
@Slf4j
public class RespositoryServiceImpl extends ServiceImpl<RespositoryMapper, RespositoryEntity> implements RespositoryService {

    @Autowired
    private RespositoryMapper respositoryMapper;

    @Override
    public PageResponse<List<QueryRespositorysResponseVo>> listRespositorys(QueryRespositorysRequestVo requestVo) {
        Page<RespositoryEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<RespositoryEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getAddress())) {
            wrapper.like("address", requestVo.getAddress());
        }
        wrapper.eq("is_del", requestVo.getIsDel());
        wrapper.orderBy("id", false);
        List<RespositoryEntity> respositoryEntities = respositoryMapper.selectPage(page, wrapper);
        List<QueryRespositorysResponseVo> responseVos = BeanUtils.convert(respositoryEntities, QueryRespositorysResponseVo.class);
        return PageResponse.newInstance(responseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    @UserOperation("新增仓库信息")
    public BaseResponse<Boolean> addRespositorys(AddResponsitorysRequestVo requestVo) {
        RespositoryEntity entity = new RespositoryEntity();
        entity.setAddress(requestVo.getAddress());
        entity.setDesc(requestVo.getDesc());
        entity.setIsDel(ObjectUtils.nullSafeEquals("on", requestVo.getIsDelStr()) ? 1 : 0);
        respositoryMapper.insert(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("修改仓库信息")
    public BaseResponse<Boolean> updateRespositorys(UpdateResponsitorysRequestVo requestVo) {
        RespositoryEntity entity = new RespositoryEntity();
        entity.setId(requestVo.getId());
        entity.setAddress(requestVo.getAddress());
        entity.setDesc(requestVo.getDesc());
        entity.setIsDel(ObjectUtils.nullSafeEquals("on", requestVo.getIsDelStr()) ? 1 : 0);
        respositoryMapper.updateById(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("删除仓库信息")
    public BaseResponse<Boolean> deleteRespositorys(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return BaseResponse.errorOf(Boolean.FALSE);
        }
        respositoryMapper.deleteBatchIds(ids);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    public BaseResponse<List<QueryRespositorysResponseVo>> queryRespositorysforSelect() {
        Wrapper<RespositoryEntity> wrapper = new EntityWrapper<>();
        List<RespositoryEntity> supplierEntities = respositoryMapper.selectList(wrapper);
        return BaseResponse.successOf(BeanUtils.convert(supplierEntities, QueryRespositorysResponseVo.class));
    }
}
